Golang : Get current time
Problem :
Need to get current time stamp in Go and display in string.
Solution :
Use the time.Local() and time.Format() functions.
getcurrenttime.go
package main
import (
"fmt"
"os"
"time"
)
func main() {
// get current timestamp
currenttime := time.Now().Local()
fmt.Println("Current time : ", currenttime.Format("2006-01-02 15:04:05 +0800"))
}
Execute the code
>go run getcurrenttime.go
and the output will be :
Current time : 2014-06-30 14:23:38 +0800
Take note that +0800
is the timezone in my current location. You can change it to suit your timezone.
Reference :
See also : Golang : Get file last modified date and time
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+14.6k Golang : Rename directory
+21.9k SSL : How to check if current certificate is sha1 or sha2
+19.4k Golang : Calculate entire request body length during run time
+23.7k Golang : Get ASCII code from a key press(cross-platform) example
+12.9k Golang : Add ASCII art to command line application launching process
+13.6k Facebook PHP getUser() returns 0
+5.1k Golang : Constant and variable names in native language
+9.1k Golang : Go as a script or running go with shebang/hashbang style
+31.7k Golang : bufio.NewReader.ReadLine to read file line by line
+10.3k Golang : Print how to use flag for your application example
+3.9k Golang : Switch Redis database redis.NewClient
+13.7k Golang : Strings comparison